package net.lookyou.boot.demo.controller;

import com.github.pagehelper.PageInfo;
import net.lookyou.boot.demo.entity.User;
import net.lookyou.boot.demo.entity.UserRole;
import net.lookyou.boot.demo.service.UserRoleService;
import net.lookyou.boot.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gemerl
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService userService;

    @Autowired
    UserRoleService userRoleService;

    /**
     * 单表分页
     * @return 返回分页数据
     */
    @RequestMapping("/page")
    public PageInfo<User> queryUserPage(){
        return userService.queryByPage(2,2);
    }

    /**
     * 多表分页
     * @return 返回分页数据
     */
    @RequestMapping("/page2")
    public PageInfo<UserRole> queryUserPage2(){
        return userRoleService.queryUserRolePage(2,2);
    }

    @RequestMapping("/list")
    public List<User> queryUserList(){
       return userService.queryUserList();
    }

    @RequestMapping("/list2")
    public List<UserRole> queryUserList2(){
        return userRoleService.queryUserList();
    }

    @RequestMapping("/get/{id}")
    public User queryUserById(@PathVariable int id){
        return userService.queryUserById(id);
    }

    @RequestMapping("/add")
    public int addUser(){
        return userService.addUser(new User(){{
            setId(1);
            setName("zs");
        }});
    }
}
